//
//  NavView.m
//  WanCaiC8
//
//  Created by tangang on 2018/5/14.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "NavView.h"
#import "NavCell.h"
#import "RankingList.h"
#import "MasterRankVC.h"
#import "TGInformationViewController.h"
#import "PlanVC.h"
#import "TGPlanViewController.h"
#import "TGMasterListViewController.h"
#import "TGTrendListViewController.h"
@interface NavView ()<UICollectionViewDelegate,UICollectionViewDataSource,UIGestureRecognizerDelegate>
@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) NSMutableArray   *dataArr;
@end

@implementation NavView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.backgroundColor=[UIColor colorWithWhite:0.0 alpha:.2];
//        self.backgroundColor = [UIColor clearColor];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(remove)];
        tap.delegate=self;
        [self addGestureRecognizer:tap];
        [self.collectionView reloadData];
    }
    return self;
}

- (void)remove{
    [self removeFromSuperview];
    if (self.removeBlock) {
        self.removeBlock();
    }
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:self.collectionView]) {
        return NO;
    }
    return YES;
}

- (void)show
{
    [UIView animateWithDuration:.5 animations:^{
        self.frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }];
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NavCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"NavCell" forIndexPath:indexPath];
    NSDictionary *dict=[self.dataArr objectAtIndex:indexPath.row];
    [cell setImage:[dict objectForKey:@"image"] andTitle:[dict objectForKey:@"title"]];
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
        {
            [[TGTools viewController:self].navigationController popToRootViewControllerAnimated:YES];
        }
            break;
        case 1:
        {
            UIViewController *selfCtrl=[TGTools viewController:self];
            if ([selfCtrl isMemberOfClass:[RankingList class]]) {
                RankingListType type=((RankingList *)selfCtrl).type;
                if (type==RankingListTypeLottery) {
                    [self remove];
                }else{
                    RankingList *vc=[[RankingList alloc]init];
                    vc.type=RankingListTypeLottery;
                    [selfCtrl.navigationController pushViewController:vc animated:YES];
                }
            }else{
                RankingList *vc=[[RankingList alloc]init];
                vc.type=RankingListTypeLottery;
                [selfCtrl.navigationController pushViewController:vc animated:YES];
            }
        }
            break;
        case 2:
        {
           
            UIViewController *selfCtrl=[TGTools viewController:self];
            if ([selfCtrl isMemberOfClass:[TGPlanViewController class]]) {
                [self remove];
            }else{
                TGPlanViewController *vc=[[TGPlanViewController alloc] init];
                vc.saveViewController=selfCtrl;
                [selfCtrl.navigationController pushViewController:vc animated:YES];
            }
        }
            break;
        case 3:
        {
            
            UIViewController *selfCtrl=[TGTools viewController:self];
            if ([selfCtrl isMemberOfClass:[TGMasterListViewController class]]) {
                [self remove];
            }else{
                TGMasterListViewController *vc=[[TGMasterListViewController alloc] init];
                vc.saveViewController=selfCtrl;
                [selfCtrl.navigationController pushViewController:vc animated:YES];
            }
        }
            break;
        case 4:
        {
            
            UIViewController *selfCtrl=[TGTools viewController:self];
            if ([selfCtrl isMemberOfClass:[TGInformationViewController class]]) {
                TGInformationType type=((TGInformationViewController *)selfCtrl).type;
                if (type==TGInformationTypeInformation) {
                    [self remove];
                }else{
                    TGInformationViewController *vc=[[TGInformationViewController alloc]init];
                    vc.type=TGInformationTypeInformation;
                    [selfCtrl.navigationController pushViewController:vc animated:YES];
                }
            }else{
                TGInformationViewController *vc=[[TGInformationViewController alloc] init];
                vc.type=TGInformationTypeInformation;
                [selfCtrl.navigationController pushViewController:vc animated:YES];
            }
        }
            break;
        case 5:
        {
            
            UIViewController *selfCtrl=[TGTools viewController:self];
            if ([selfCtrl isMemberOfClass:[TGInformationViewController class]]) {
                TGInformationType type=((TGInformationViewController *)selfCtrl).type;
                if (type==TGInformationTypePicture) {
                    [self remove];
                }else{
                    TGInformationViewController *vc=[[TGInformationViewController alloc]init];
                    vc.type=TGInformationTypeInformation;
                    [selfCtrl.navigationController pushViewController:vc animated:YES];
                }
            }else{
                TGInformationViewController *vc=[[TGInformationViewController alloc] init];
                vc.type=TGInformationTypePicture;
                [selfCtrl.navigationController pushViewController:vc animated:YES];
            }
            
        }
            break;
        case 6:
        {
            
            UIViewController *selfCtrl=[TGTools viewController:self];
            
            if ([selfCtrl isMemberOfClass:[TGTrendListViewController class]]) {
                [self remove];
            }else{
         
                TGTrendListViewController * vc = [[TGTrendListViewController alloc] init];
                vc.saveViewController=selfCtrl;
                [selfCtrl.navigationController pushViewController:vc animated:YES];
            }
            
        }
            break;
        default:
            break;
    }
    [self remove];
}

#pragma mark -- 懒加载
- (UICollectionView *)collectionView{
    if (_collectionView==nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake(SCREEN_WIDTH/1.5, real_size(50))];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor=[UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator=NO;
        [_collectionView registerClass:[NavCell class] forCellWithReuseIdentifier:@"NavCell"];
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.top.equalTo(self);
            make.right.equalTo(self);
            make.left.equalTo(self).with.offset(SCREEN_WIDTH/3);
        }];
    }
    return _collectionView;
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[[NSMutableArray alloc]init];
        [_dataArr addObject:[[NSDictionary alloc]initWithObjectsAndKeys:@"首页",@"title",[UIImage imageNamed:@"home"],@"image", nil]];
        [_dataArr addObject:[[NSDictionary alloc]initWithObjectsAndKeys:@"开奖直播",@"title",[UIImage imageNamed:@"open"],@"image", nil]];
        [_dataArr addObject:[[NSDictionary alloc]initWithObjectsAndKeys:@"官方计划",@"title",[UIImage imageNamed:@"plan"],@"image", nil]];
        [_dataArr addObject:[[NSDictionary alloc]initWithObjectsAndKeys:@"高手计划",@"title",[UIImage imageNamed:@"master"],@"image", nil]];
        [_dataArr addObject:[[NSDictionary alloc]initWithObjectsAndKeys:@"最新资讯",@"title",[UIImage imageNamed:@"information"],@"image", nil]];
        [_dataArr addObject:[[NSDictionary alloc]initWithObjectsAndKeys:@"最新图库",@"title",[UIImage imageNamed:@"pictures"],@"image", nil]];
        [_dataArr addObject:[[NSDictionary alloc]initWithObjectsAndKeys:@"走势分析",@"title",[UIImage imageNamed:@"trend"],@"image", nil]];
    }
    return _dataArr;
}

@end
